﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	public partial class FormSel : System.Windows.Forms.Form
	{
		public FormSel()
		{
			this.InitializeComponent();
		}

		public FormSel(ExternalCommandData cmdData, string msg, ElementSet elements, ICollection<Element> collection, string strCurrApp)
		{
			this.InitializeComponent();
			this.cmdDataForm = cmdData;
			this.msgForm = msg;
			this.elementsForm = elements;
			this.frmCollection = collection;
			this.currentApp = strCurrApp;
			this.BindPro();
			this.BindFileName();
		}

		private void BindFileName()
		{
			this.sPath = Path.GetDirectoryName(this.sPath);
			this.sPath = Path.GetDirectoryName(this.sPath);
			this.sFileName = this.sPath + "\\Data\\SelSet.xml";
		}

		private void BindPro()
		{
			if (this.cmdDataForm.Application.ActiveUIDocument == null)
			{
				return;
			}
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
			if (activeUIDocument.Document.Title != "")
			{
				this.proTitle = activeUIDocument.Document.Title;
			}
			if (activeUIDocument.Document.PathName != "")
			{
				this.proPath = activeUIDocument.Document.PathName;
			}
			XmlInfo.RegForm(this.proTitle, this.proPath);
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel1, e);
		}

		private void btnDropDown_Enter(object sender, EventArgs e)
		{
			this.textBox1.Focus();
		}

		private void btnDropDown_Click(object sender, EventArgs e)
		{
			this.ShowFrmList();
		}

		private void ShowFrmList()
		{
			this.list.Close();
			if (XmlInfo.SelIsNull(this.proPath, this.proTitle))
			{
				return;
			}
			this.list = new frmList(this.cmdDataForm, this.msgForm, this.elementsForm, this.frmCollection, this.currentApp);
			this.list.Owner = this;
			frmList frmList = this.list;
			frmList.classFirstChange = (frmList.delegateFirstChange)Delegate.Combine(frmList.classFirstChange, new frmList.delegateFirstChange(this.LoadFirst));
			System.Drawing.Point point = base.PointToScreen(this.panel1.Location);
			this.list.Top = point.Y;
			this.list.Left = point.X;
			this.list.Show();
		}

		private void FormSel_Move(object sender, EventArgs e)
		{
			System.Drawing.Point point = base.PointToScreen(this.panel1.Location);
			this.list.Top = point.Y;
			this.list.Left = point.X;
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			try
			{
				this.frmCreate.Close();
				this.frmCreate = new FormSelCreate(this.cmdDataForm, this.msgForm, this.elementsForm, this.frmCollection, this.currentApp);
				FormSelCreate formSelCreate = this.frmCreate;
				formSelCreate.classSel = (FormSelCreate.delegateSel)Delegate.Combine(formSelCreate.classSel, new FormSelCreate.delegateSel(this.ShowFrmList));
				this.frmCreate.Owner = this;
				this.frmCreate.Top = base.Top;
				this.frmCreate.Left = base.Left + 381;
				this.frmCreate.Show();
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("程序异常。");
			}
		}

		private void ShowFirst(bool b)
		{
			this.chkFirst.Visible = b;
			this.txtFirst.Visible = b;
			this.btnFirstShow.Visible = b;
			this.btnFirstLock.Visible = b;
			this.btnDropDown.Visible = b;
			this.txtInfo.Visible = !b;
			this.textBox1.Focus();
		}

		private void FormSel_Load(object sender, EventArgs e)
		{
			try
			{
				this.timer1.Start();
				this.BindLevel();
				this.xmlStr = XmlInfo.GetSelSetStr(this.sFileName, this.proPath, this.proTitle);
				if (this.xmlStr == "" || this.xmlStr == null)
				{
					this.ShowFirst(false);
				}
				else
				{
					this.ShowFirst(true);
					XmlInfo.AddSelSet(this.proPath, this.proTitle, this.xmlStr);
					this.LoadFirst();
					this.btnNew.Focus();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void ReLoad()
		{
		}

		public void LoadFirst()
		{
			try
			{
				XmlNodeList xmlNodeList = this.GetXmlDoc().SelectNodes("Elements/Select");
				if (xmlNodeList.Count == 0)
				{
					this.ShowFirst(false);
				}
				else
				{
					this.ShowFirst(true);
					if (xmlNodeList.Item(0).Attributes["Check"].Value == "true")
					{
						this.chkFirst.Checked = true;
					}
					else
					{
						this.chkFirst.Checked = false;
					}
					this.txtFirst.Text = xmlNodeList.Item(0).Attributes["Name"].Value;
					if (xmlNodeList.Item(0).Attributes["Show"].Value == "true")
					{
						this.btnFirstShow.ImageIndex = 0;
					}
					else
					{
						this.btnFirstShow.ImageIndex = 1;
					}
					if (xmlNodeList.Item(0).Attributes["Lock"].Value == "true")
					{
						this.btnFirstLock.ImageIndex = 2;
					}
					else
					{
						this.btnFirstLock.ImageIndex = 3;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private XmlDocument GetXmlDoc()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.GetSelSet(this.proPath, this.proTitle));
			return xmlDocument;
		}

		private void FormSel_FormClosing(object sender, FormClosingEventArgs e)
		{
			XmlInfo.SetSelSetStr(this.sFileName, this.proPath, this.proTitle, XmlInfo.GetSelSet(this.proPath, this.proTitle));
			XmlInfo.DeleteSelSet(this.proPath, this.proTitle);
			XmlInfo.FormLogout(this.proTitle, this.proPath);
		}

		private void txtInfo_TextChanged(object sender, EventArgs e)
		{
		}

		private void ChangeFirst(string attrName, string attrValue)
		{
			XmlDocument xmlDoc = this.GetXmlDoc();
			xmlDoc.SelectNodes("Elements/Select").Item(0).Attributes[attrName].Value = attrValue;
			XmlInfo.AddSelSet(this.proPath, this.proTitle, xmlDoc.InnerXml);
		}

		private void chkFirst_CheckedChanged(object sender, EventArgs e)
		{
			try
			{
				base.Enabled = false;
				if (this.chkFirst.Checked)
				{
					this.ChangeFirst("Check", "true");
					CmdTools.cmdSelect(this.cmdDataForm, this.txtFirst.Text);
				}
				else
				{
					this.ChangeFirst("Check", "false");
				}
				base.Enabled = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
				base.Enabled = true;
			}
		}

		private void btnFirstShow_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.btnFirstShow.ImageIndex == 0)
				{
					this.btnFirstShow.ImageIndex = 1;
					this.ChangeFirst("Show", "false");
					CmdTools.cmdShow(this.cmdDataForm, this.txtFirst.Text, false);
				}
				else
				{
					this.btnFirstShow.ImageIndex = 0;
					this.ChangeFirst("Show", "true");
					CmdTools.cmdShow(this.cmdDataForm, this.txtFirst.Text, true);
				}
				this.textBox1.Focus();
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
			}
		}

		private void btnFirstLock_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.btnFirstLock.ImageIndex == 2)
				{
					this.btnFirstLock.ImageIndex = 3;
					this.ChangeFirst("Lock", "false");
					CmdTools.cmdLock(this.cmdDataForm, this.txtFirst.Text, false);
				}
				else
				{
					this.btnFirstLock.ImageIndex = 2;
					this.ChangeFirst("Lock", "true");
					CmdTools.cmdLock(this.cmdDataForm, this.txtFirst.Text, true);
				}
				this.textBox1.Focus();
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
			}
		}

		private void txtFirst_TextChanged(object sender, EventArgs e)
		{
			try
			{
				this.ChangeFirst("Name", this.txtFirst.Text);
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				Result result = CmdTools.FrmAddToColl(this.cmdDataForm);
				if (result == null)
				{
					MessageBox.Show("成功添加到选择集！");
				}
                else if ((int)result != 1)
				{
					MessageBox.Show("操作失败！");
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
			}
		}

		public bool FirstIsCheck
		{
			get
			{
				return this.chkFirst.Checked;
			}
			set
			{
				this.chkFirst.Checked = value;
			}
		}

		private void BindLevel()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.xmlLevelStr);
			XmlElement documentElement = xmlDocument.DocumentElement;
			documentElement.RemoveAll();
			for (int i = 0; i < this.frmCollection.Count; i++)
			{
				Element element = this.frmCollection.ElementAt(i);
				XmlElement xmlElement = xmlDocument.CreateElement("Level");
				xmlElement.SetAttribute("Check", "false");
				xmlElement.SetAttribute("Name", element.Name);
				documentElement.AppendChild(xmlElement);
			}
			XmlInfo.xmlLevelStr = xmlDocument.InnerXml;
		}

		private void btnReduce_Click(object sender, EventArgs e)
		{
			try
			{
				Result result = CmdTools.FrmMinusColl(this.cmdDataForm);
				if (result == null)
				{
					MessageBox.Show("成功从选择集中去除！");
				}
                else if ((int)result != 1)
				{
					MessageBox.Show("操作失败！");
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				XmlInfo.selDelete(this.proPath, this.proTitle);
				this.LoadFirst();
				this.ShowFrmList();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void btn3D_Click(object sender, EventArgs e)
		{
			try
			{
				base.Enabled = false;
				CmdTools.Sel3D(this.cmdDataForm);
				base.Enabled = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
				base.Enabled = true;
			}
		}

		private void btnAnti_Click(object sender, EventArgs e)
		{
			try
			{
				base.Enabled = false;
				CmdTools.FrmAntiSelect(this.cmdDataForm);
				base.Enabled = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
				base.Enabled = true;
			}
		}

		private void btnSel_Click(object sender, EventArgs e)
		{
			try
			{
				base.Enabled = false;
				CmdTools.FrmSelSetSelect(this.cmdDataForm);
				base.Enabled = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
				base.Enabled = true;
			}
		}

		private void txtInfo_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private void txtFirst_Leave(object sender, EventArgs e)
		{
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
			if (System.Windows.Forms.Form.ActiveForm != null)
			{
				if (this.proTitle != activeUIDocument.Document.Title || this.proPath != activeUIDocument.Document.PathName)
				{
					base.Hide();
					return;
				}
				base.Show();
				return;
			}
			else
			{
				if (WinFormTools.GetForegroundWindow().ToString() != this.currentApp)
				{
					base.Hide();
					return;
				}
				if (this.cmdDataForm.Application.ActiveUIDocument == null)
				{
					base.Close();
					return;
				}
				if (activeUIDocument.Document.Title != this.proTitle)
				{
					base.Hide();
					return;
				}
				bool flag = !(activeUIDocument.Document.PathName != this.proPath);
				if (flag)
				{
					if (!base.Visible)
					{
						base.Show();
						return;
					}
				}
				else
				{
					base.Hide();
				}
				return;
			}
		}

		private ExternalCommandData cmdDataForm;

		private string msgForm;

		private ElementSet elementsForm = new ElementSet();

		private ICollection<Element> frmCollection;

		private string proTitle = "";

		private string proPath = "";

		private string sPath = Assembly.GetExecutingAssembly().Location;

		private string sFileName = "";

		private string xmlStr = "";

		private string currentApp = "";

		private frmList list = new frmList();

		private FormSelCreate frmCreate = new FormSelCreate();
	}
}
